package com.cn.jdia.dc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cn.jdia.dc.context.SpringContextUtils;
import com.cn.jdia.dc.model.ServerHeartbeat;
import com.cn.jdia.dc.model.Serverconfig;
import com.cn.jdia.dc.model.Serverdisk;
import com.cn.jdia.dc.model.Serverquota;
import com.cn.jdia.dc.service.IServerconfigService;
import com.cn.jdia.dc.service.IServerdiskService;
import com.cn.jdia.dc.service.IServerheartbeatService;
import com.cn.jdia.dc.service.IServerquotaService;
import com.google.gson.Gson;

@Controller
public class ServerMonitorController {

	@Resource
	private IServerconfigService svService;

	@Resource
	private IServerdiskService diskService;

	@Resource
	private IServerquotaService quotaService;

	@Resource
	private IServerheartbeatService heartbeatService;

	@RequestMapping(value = "/servermonitor")
	@ResponseBody
	public ModelAndView dbmonitor() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("svmonitor");

		List<Serverconfig> allServerConfig = svService.getAllServerConfig();

		List<Map<String, String>> svlist = new ArrayList<>();
		Map<String, String> map = null;
		for (int i = 0; i < allServerConfig.size(); i++) {
			map = new HashMap<>();
			map.put("sname", allServerConfig.get(i).getServername());
			map.put("ip", allServerConfig.get(i).getIp());
			svlist.add(map);
		}

		mav.addObject("list", svlist);

		List<Serverdisk> diskDetails = diskService.queryServerdiskByServer(svlist.get(0).get("sname"),
				svlist.get(0).get("ip"));

		List<Map<String, String>> diskList = new ArrayList<>();
		Map<String, String> diskMap;
		for (Serverdisk disk : diskDetails) {
			diskMap = new HashMap<>();
			diskMap.put("diskname", disk.getDiskname());
			diskMap.put("used", disk.getDiskused().replaceAll("%", ""));

			diskList.add(diskMap);
		}

		mav.addObject("disklist", diskList);

		Serverquota quota = quotaService.getServerquotaByServer(svlist.get(0).get("sname"), svlist.get(0).get("ip"));

		Map<String, String> quotaMap = new HashMap<>();
		quotaMap.put("totalmem", quota.getMemtotal() + "");
		quotaMap.put("usedmem", quota.getMemused() + "");
		quotaMap.put("cpu1", quota.getCpu1());
		quotaMap.put("cpu2", quota.getCpu2());
		quotaMap.put("cpu3", quota.getCpu3());
		quotaMap.put("cpu4", quota.getCpu4());
		quotaMap.put("cpu5", quota.getCpu5());

		Gson gson = new Gson();
		String json = gson.toJson(quotaMap);

		mav.addObject("quotaMap", json);
		mav.addObject("mem", quotaMap);

		return mav;
	}

	@RequestMapping(value = "/serverselect")
	@ResponseBody
	public String serverSelect(String nameAndIp) {
		// System.out.println(nameAndIp);
		String[] split = nameAndIp.split(" ");

		// System.out.println(split[0]);
		// System.out.println(split[1]);

		Map<String, String> map = new HashMap<>();

		Serverquota quota = quotaService.getServerquotaByServer(split[0], split[1]);
		List<Serverdisk> diskDetails = diskService.queryServerdiskByServer(split[0], split[1]);

		Gson gson = new Gson();
		String quotaData = gson.toJson(quota);
		String diskData = gson.toJson(diskDetails);

		map.put("quota", quotaData);
		map.put("disk", diskData);

		List<ServerHeartbeat> hbList = heartbeatService.getServerHeartbeat(split[0], 10);
		map.put("hcount", String.valueOf(hbList.size()));

		List<String> heartBeat = new ArrayList<>();
		for (ServerHeartbeat hb : hbList) {
			// System.out.println(hb.getHeartbeat());
			heartBeat.add(hb.getHeartbeat());
			// heartBeat.add(hb.getHeartbeat().substring(hb.getHeartbeat().indexOf("-")+1));
		}

		map.put("heartbeat", gson.toJson(heartBeat));

		return gson.toJson(map);
	}
}
